<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box{
            width: 200px;
            height: 200px;
            /* margin: 100 auto;   */
            border: 1px solid #000;
            font-size: 30px;
            text-align: center;
            line-height: 200px;
        }
        #btn{
            width: 202px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <button id="btn">
        点击换颜色
    </button>
    <script>
        // ## 3.封装一个十六进制的随机颜色 函数  #fff111

        btn.onclick = function(){
            function fn(i,j){
                var min,max
                min = i < j ? i : j
                max = i > j ? i : j
                
                var k = parseInt(Math.random() * (max - min + 1) + min)

                if(k >= 16){
                    k = k.toString(16)
                    // console.log(k)
                }else{
                    k = '0' + k.toString(16)
                    // console.log(k)
                }
                return k
            }
            box.style.background = '#' + fn(0,255) + fn(0,255) + fn(0,255)
        }
    </script>
</body>
</html>